import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from '../views/Layout.vue'
import Cookies from "js-cookie";

//安装路由
Vue.use(VueRouter)


const routes = [

  //  ====  登录  ====
  {
    path: '/login',
    name:'Login',
    component: () => import('@/views/login/Login.vue')
  },
  //  ====  主页  ====
  {
    path: '/',
    name: 'Layout',
    component: Layout,
    redirect:'/home',
    children:[
      //  ====  主页Home  ====
      {path: 'home', name: 'Home', component: () => import('@/views/home/HomeView.vue'),},
      //  ====  User  ====
      {path: 'userList', name: 'userList', component: () => import('@/views/user/ListUser.vue'),},
      {path: 'addUser', name: 'AddUser', component: () => import('@/views/user/AddUser.vue'),},
      {path: 'editUser', name: 'EditUser', component: () => import('@/views/user/EditUser.vue'),},
      //  ====  Admin  ====
      {path: 'adminList', name: 'adminList', component: () => import('@/views/admin/List.vue'),},
      {path: 'addAdmin', name: 'AddAdmin', component: () => import('@/views/admin/Add.vue'),},
      {path: 'editAdmin', name: 'EditAdmin', component: () => import('@/views/admin/Edit.vue'),},
      //  ====  Category  ====
      {path: 'CategoryList', name: 'CategoryList', component: () => import('@/views/category/List'),},
      {path: 'addCategory', name: 'AddCategory', component: () => import('@/views/category/Add.vue'),},
      {path: 'editCategory', name: 'EditCategory', component: () => import('@/views/category/Edit.vue'),},
      //  ====  Book  ====
      {path: 'bookList', name: 'BookList', component: () => import('@/views/book/List'),},
      {path: 'addBook', name: 'AddBook', component: () => import('@/views/book/Add.vue'),},
      {path: 'editBook', name: 'EditBook', component: () => import('@/views/book/Edit.vue'),},
      //  ====  Borrow  ====
      {path: 'borrowList', name: 'BorrowList', component: () => import('@/views/borrow/List'),},
      {path: 'addBorrow', name: 'AddBorrow', component: () => import('@/views/borrow/Add.vue'),},
      {path: 'editBorrow', name: 'EditBorrow', component: () => import('@/views/borrow/Edit.vue'),},
      //  ====  revert  ====
      {path: 'RevertList',name:'RevertList',component:() => import("@/views/revert/revert.vue")}
    ]
  },
  {
    path:'*',
    name:404,
    component: () => import('@/views/404.vue')
  }
]


const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
//配置路由守卫
router.beforeEach((to,from,next)=>{
  if (to.path === '/login') next()
  const admin = Cookies.get('admin')
  if (!admin && to.path !== "/login") return next('/login')  //强制退回
  next()
})

//配置导出路由
export default router

// 针对ElementUI导航栏中重复导航报错问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
